package cn.com.xyc.action;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.util.SavedRequest;
import org.apache.shiro.web.util.WebUtils;
import org.apache.struts2.ServletActionContext;

import cn.com.xyc.pojo.Admin;


public class MainAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Admin admin;//登录人
	private String successUrl = "main!home";
	
	public String execute() throws Exception {
		return SUCCESS;
	}
	public String login()throws Exception{
		if(admin==null){
		  return null;	
		}
		Subject subject=SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(
				admin.getUsername(), admin.getPassword());
		try{
			if(!subject.isAuthenticated()){
				subject.login(token);
			}
		} catch (UnknownAccountException uae) {
			addActionError("用户不存在");
			return SUCCESS;
		} catch (IncorrectCredentialsException ice) {
			addActionError("PUserID错误");
			return SUCCESS;
		} catch (LockedAccountException lae) {
			addActionError("该账户已锁");
			return SUCCESS;
		} catch (AuthenticationException ae) {
			addActionError("账户登陆异常");
			return SUCCESS;
		}
		SavedRequest saveRequest=WebUtils.getAndClearSavedRequest(ServletActionContext.getRequest());
		if (saveRequest != null) {
			successUrl = saveRequest.getRequestUrl();
			successUrl=successUrl.substring(successUrl.indexOf("/", 1) + 1,
					successUrl.length());
		}
		return "loginSuccess";
	}
	public String navbar()throws Exception{
		return "navbar";
	}
	
	public String menu()throws Exception {
		return "menu";
	}
	
	public String home(){
		return "home";
	}
	
	public Admin getAdmin() {
		return admin;
	}
	public void setAdmin(Admin admin) {
		this.admin = admin;
	}
	public String getSuccessUrl() {
		return successUrl;
	}
	public void setSuccessUrl(String successUrl) {
		this.successUrl = successUrl;
	}
	
	
}
